package org.mspring.platform.security.dao.impl;

import java.util.List;

import org.mspring.platform.dao.hibernate.GenericDao;
import org.mspring.platform.security.dao.RoleDao;
import org.mspring.platform.security.entity.Role;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Gao Youbo
 * @since 2014-01-11
 */
@Repository
public class RoleDaoImpl extends GenericDao<Role, Long> implements RoleDao {

    @Override
    public List<Role> listRoleByUser(long userId) {
        return list("select userRole.PK.role from UserRole userRole where userRole.PK.user.id = ?", userId);
    }

    @Override
    public int countUserByRole(long roleId) {

        return getHibernateTemplate().count("select count(*) from UserRole userRole where userRole.PK.role.id = ?", roleId);
    }

    @Override
    public List<Role> listEnabledRole() {
        return list("select role from Role role where role.enabled = ?", true);
    }

    @Override
    public Role findByName(String name) {
        return uniqueResult("select r from Role r where r.name = ?", name);
    }
}
